package com.shuoyue.ycgk.ui.papergroups.running.myrush;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.Paper;
import com.shuoyue.ycgk.views.TabLayoutUtil;
import com.shuoyue.ycgk.views.viewpager_scroll.SuperViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserRushActivity extends BaseMvpActivity<UserRushContract.Presenter>
        implements UserRushContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    SuperViewPager viewPager;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    protected IndexFragmentStateAdapter indexFragmentAdapter;
    protected List<BaseMvpFragment> fragmentMainItems;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.lay_empty)
    FrameLayout layEmpty;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_rush;
    }

    @Override
    protected void initView() {
        pageTitle.setText("我的考前冲刺");
        title.setText("加载中");
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> mPresenter.getRushList());
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new UserRushContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getRushList();

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setPaperSetInfo(List<RushBean> rushBeans) {
        if (rushBeans == null || rushBeans.size() == 0) {
            title.setText("没有购买考前冲刺");
        } else {
            layEmpty.setVisibility(View.GONE);
            fragmentMainItems = new ArrayList<>();
            for (RushBean topType : rushBeans) {
                fragmentMainItems.add(FragmentUserRush.getInstance(topType.getModule().getName(),
                        (ArrayList<Paper>) topType.getTestPaperList()));
            }
            indexFragmentAdapter = new IndexFragmentStateAdapter(getSupportFragmentManager(), fragmentMainItems);
            tabLayout.setupWithViewPager(viewPager);
            viewPager.setOffscreenPageLimit(1);
            viewPager.setAdapter(indexFragmentAdapter);
            refreshLayout.setEnableRefresh(false);
            TabLayoutUtil tabLayoutUtil = new TabLayoutUtil();
            tabLayoutUtil.setTabTextSizeWhenSelect(tabLayout, rushBeans);
        }
    }


}
